CC.GUIC.printlnToOutput("executing JetEvents.js");

var JetSystem = {

    // Predefined string constants for all event kinds
    EVENT_TYPE_ONSTEP: "onStep",
    EVENT_TYPE_ONPREPAINT: "onPrePaint",
    EVENT_TYPE_ONPOSTPAINT: "onPostPaint",
    EVENT_TYPE_ONKEYUP: "onKeyUp",
    EVENT_TYPE_ONKEYDOWN: "onKeyDown",
    EVENT_TYPE_ONKEYTYPED: "onKeyTyped",
    EVENT_TYPE_ONBODYCOLLISION: "onBodyCollision",

    // Simulation step listeners
    // They are called between zeroing forces and accumulating engine forces
    onStepListeners: [],

    // Simulation paint listeners
    // They are called just before standard scene painting take place
    onPrePaintListeners: [],

    // Simulation paint listeners
    // They are called right after standard scene painting take place
    onPostPaintListeners: [],

    // Keyboard keys listeners
    // They are called when keys gets pressed or released
    onKeyUpListeners: [],
    onKeyDownListeners: [],
    onKeyTypedListeners: [],

    // Body collision listeners
    onBodyCollisionListeners: [],

    addEventListener: function(_type, _callback) {
        switch (_type) {
            case this.EVENT_TYPE_ONSTEP :
                if (_callback instanceof Function)
                    this.onStepListeners.push(_callback);
                break;
            case this.EVENT_TYPE_ONPREPAINT :
                if (_callback instanceof Function)
                    this.onPrePaintListeners.push(_callback);
                break;
            case this.EVENT_TYPE_ONPOSTPAINT :
                if (_callback instanceof Function)
                    this.onPostPaintListeners.push(_callback);
                break;
            case this.EVENT_TYPE_ONKEYUP :
                if (_callback instanceof Function)
                    this.onKeyUpListeners.push(_callback);
                break;
            case this.EVENT_TYPE_ONKEYDOWN :
                if (_callback instanceof Function)
                    this.onKeyDownListeners.push(_callback);
                break;
            case this.EVENT_TYPE_ONKEYTYPED :
                if (_callback instanceof Function)
                    this.onKeyTypedListeners.push(_callback);
                break;
            case this.EVENT_TYPE_ONBODYCOLLISION :
                if (_callback instanceof Function)
                    this.onBodyCollisionListeners.push(_callback);
                break;
        }
    },

    /**
     * Notifies all onStep listeners.
     * There is single parameter past to listeners: simulation elapsed time.
     */
    callOnStepListeners: function() {
        for(var i=0; i<this.onStepListeners.length; i++)
            this.onStepListeners[i](/*Scene.getTime()*/);
    },

    callOnPrePaintListeners: function() {
        for(var i=0; i<this.onPrePaintListeners.length; i++)
            this.onPrePaintListeners[i]();
    },
    
    callOnPostPaintListeners: function() {
        for(var i=0; i<this.onPostPaintListeners.length; i++)
            this.onPostPaintListeners[i]();
    },
    
    callOnKeyUpListeners: function(_keyName, _keyCode) {
        for(var i=0; i<this.onKeyUpListeners.length; i++)
            this.onKeyUpListeners[i](_keyName, _keyCode);
    },
    
    callOnKeyDownListeners: function(_keyName, _keyCode) {
        for(var i=0; i<this.onKeyDownListeners.length; i++)
            this.onKeyDownListeners[i](_keyName, _keyCode);
    },

    callOnKeyTypedListeners: function(_keyName, _keyCode) {
        for(var i=0; i<this.onKeyTypedListeners.length; i++)
            this.onKeyTypedListeners[i](_keyName, _keyCode);
    },

    callOnBodyCollisionListeners: function(_bodyA, _bodyB, _collisionIndex) {
        for(var i=0; i<this.onBodyCollisionListeners.length; i++)
            this.onBodyCollisionListeners[i](_bodyA, _bodyB, _collisionIndex);
    }

//    callOnBodyCollisionListeners: function() {
//        for(var i=0; i<this.onBodyCollisionListeners.length; i++)
//            this.onBodyCollisionListeners[i]();
//    }
}